var express = require('express');
const createHttpError = require('http-errors');
var router = express.Router();
var axios = require('axios')

var { fanli, AdminUser, tousu, huifu, tiWen, GongGao, keyword } = require('../untils/model')
var { createToken, checkToken } = require("../untils/token")


router.post("/login", (req, res) => {
    var body = req.body
    AdminUser.findOne({ ...body }).then(result => {
        if (result) {
            var token = createToken({ username: body.username, phone: result.phone, role: result.role })
            res.json({
                msg: "登录成功",
                code: 200,
                result,
                token,
                type: 1
            })
        } else {
            res.json({
                msg: "账号或密码错",
                code: 403,
                type: 0
            })
        }
    }).catch(err => {
        console.log(err)
        res.json({
            err,
            msg: "服务器异常",
            code: 503,
            type: 0
        })
    })
})


// 发送验证码
router.get("/sent", (req, res) => {
    var query = req.query;
    AdminUser.findOne({
        phone: query.phone,
    }).then(result => {
        console.log(result)
        if (result) {
            // 才能发送 
            axios.get("http://47.104.209.44:3333/captcha/sent", {
                params: {
                    phone: query.phone
                }
            }).then(obj => {
                if (obj.data.code == 200) {
                    res.json({
                        code: 200,   // 402  403 统一失败  
                        msg: "验证码发送成功",
                        type: 1,
                    })
                } else {
                    res.json({
                        code: 402,
                        msg: "验证码发送失败-超过次数",
                        type: 0,
                    })
                }
            })
        } else {
            res.json({
                code: 402,
                msg: "手机号码未注册",
                type: 0,
            })
        }
    }).catch(err => {
        res.json({
            code: 500,   // 402  403 统一失败  
            msg: "服务器异常- 未知错误",
            type: 0,
        })
    })
})


// 校验验证码 
router.get("/verify", (req, res) => {
    const { phone, captcha } = req.query;
    axios.get("http://47.104.209.44:3333/captcha/verify", {
        params: {
            phone,
            captcha
        }
    }).then(result => {
        let { code } = result.data;
        console.log(result.data)
        if (code == 200) {
            // token 

            var token = createToken({
                phone: phone,
                captcha: captcha
            })
            res.json({
                code: 200,
                msg: "登录成功",
                type: 1,
                phone,
                token
            })
        } else {
            res.json({
                code: 402,   // 402  403 统一失败  
                msg: "校验失败,请重新登录",
                type: 0,
            })
        }
    }).catch(err => {
        res.json({
            code: 500,   // 500 统一失败  
            msg: "服务器异常- 未知错误",
            type: 0,
        })
    })
})



// 获取用户信息
router.get("/getuserinfo", (req, res) => {

    // 判断身份的有效性 
    checkToken(req, res, ({ phone }) => {

        AdminUser.findOne({
            phone
        }).then(result => {
            res.json({
                code: 200,
                msg: "获取用户信息数据成功",
                type: 1,
                result
            })
        }).catch(err => {
            res.json({
                code: 500,   // 500 统一失败  
                msg: "服务器异常- 未知错误",
                type: 0,
            })
        })
    })
})

// 查询
router.get("/getuserlist", (req, res) => {
    var query = req.query;
    AdminUser.find()
        .sort({ _id: -1 })
        .then(result => {
            res.json({
                msg: "查询用户列表成功",
                code: 200,
                result,
                type: 1,
            })
        }).catch(err => {
            console.log(err)
            res.json({
                err,
                msg: "服务器异常",
                code: 503,
                type: 0
            })
        })
})


// 查询上级代理
router.get("/gettopdl", (req, res) => {
    var query = req.query;
    AdminUser.find({ username: query.username })
        .then(result => {
            res.json({
                msg: "上级代理查询成功",
                code: 200,
                result,
                type: 1,
            })
        }).catch(err => {
            console.log(err)
            res.json({
                err,
                msg: "服务器异常",
                code: 503,
                type: 0
            })
        })
})



//查询 所有用户
router.get("/getdaili", (req, res) => {
    var query = req.query;
    console.log(query)
    AdminUser.find()
        .then(result => {
            res.json({
                msg: "查询用户成功",
                code: 200,
                result,
                type: 1,
            })
        }).catch(err => {
            console.log(err)
            res.json({
                err,
                msg: "服务器异常",
                code: 503,
                type: 0
            })
        })
})

//查询 所有代理
router.get("/getalldaili", (req, res) => {
    var query = req.query;
    console.log(query)
    AdminUser.find({ $or: [{ 'role': '1' }, { 'role': "2" }, { 'role': "3" }] })
        .then(result => {
            res.json({
                msg: "查询代理成功",
                code: 200,
                result,
                type: 1,
            })
        }).catch(err => {
            console.log(err)
            res.json({
                err,
                msg: "服务器异常",
                code: 503,
                type: 0
            })
        })
})

//查询 所有用户
router.get("/getalluser", (req, res) => {
    var query = req.query;
    console.log(query)
    AdminUser.find({ role: 0 })
        .then(result => {
            res.json({
                msg: "查询用户成功",
                code: 200,
                result,
                type: 1,
            })
        }).catch(err => {
            console.log(err)
            res.json({
                err,
                msg: "服务器异常",
                code: 503,
                type: 0
            })
        })
})

//查询 我的用户
router.get("/getmyuser", (req, res) => {
    var query = req.query;
    console.log(query)
    AdminUser.find({ alone: query.alone, role: 0 })
        .then(result => {
            res.json({
                msg: "用户查询成功",
                code: 200,
                result,
                type: 1,
            })
        }).catch(err => {
            console.log(err)
            res.json({
                err,
                msg: "服务器异常",
                code: 503,
                type: 0
            })
        })
})

//查询 单个用户
router.get("/getonceuser", (req, res) => {
    var query = req.query;
    console.log(query)
    AdminUser.find({ username: query.username })
        .then(result => {
            res.json({
                msg: "用户查询成功",
                code: 200,
                result,
                type: 1,
            })
        }).catch(err => {
            console.log(err)
            res.json({
                err,
                msg: "服务器异常",
                code: 503,
                type: 0
            })
        })
})

//查询 我的用户and代理
router.get("/gettongjidaili", (req, res) => {
    var query = req.query;
    console.log(query)
    AdminUser.find({ alone: query.alone })
        .then(result => {
            res.json({
                msg: "下级代理查询成功",
                code: 200,
                result,
                type: 1,
            })
        }).catch(err => {
            console.log(err)
            res.json({
                err,
                msg: "服务器异常",
                code: 503,
                type: 0
            })
        })
})


//查询 同级代理
router.get("/getmydaili", (req, res) => {
    var query = req.query;
    console.log(query)
    AdminUser.find({ role: 1, alone: query.alone })
        .then(result => {
            res.json({
                msg: "代理查询成功",
                code: 200,
                result,
                type: 1,
            })
        }).catch(err => {
            console.log(err)
            res.json({
                err,
                msg: "服务器异常",
                code: 503,
                type: 0
            })
        })
})

//添加卡密

// 插入 公告 
router.post("/addkeyword", (req, res) => {
    var body = req.body;
    console.log(body)
    keyword.insertMany({
        username: body.username,
        price: body.price,
        time: new Date(),
        isuse: 0,
        key: body.key,

    }).then(result => {
        res.json({
            code: 200,   // 402  403 统一失败  
            msg: "生成卡密成功",
            type: 1,
            result
        })
    }).catch(err => {
        console.log(err)
        res.json({
            code: 500,   // 402  403 统一失败  
            msg: "服务器异常- 未知错误",
            type: 0,
            err
        })
    })

})


//查询 卡密
router.get("/getuserkey", (req, res) => {
    var query = req.query;
    console.log(query)
    keyword.find({ username: query.username }).sort({ time: -1 })
        .then(result => {
            res.json({
                msg: "卡密查询成功",
                code: 200,
                result,
                type: 1,
            })
        }).catch(err => {
            console.log(err)
            res.json({
                err,
                msg: "服务器异常",
                code: 503,
                type: 0
            })
        })
})


//管理查询 卡密
router.get("/getallkey", (req, res) => {
    var query = req.query;
    console.log(query)
    keyword.find().sort({ time: -1 })
        .then(result => {
            res.json({
                msg: "卡密查询成功",
                code: 200,
                result,
                type: 1,
            })
        }).catch(err => {
            console.log(err)
            res.json({
                err,
                msg: "服务器异常",
                code: 503,
                type: 0
            })
        })
})

//只看已用 管理
router.get("/onlyshowusedall", (req, res) => {
    var query = req.query;
    console.log(query)
    keyword.find({ isuse: 1 })
        .then(result => {
            res.json({
                msg: "卡密查询成功",
                code: 200,
                result,
                type: 1,
            })
        }).catch(err => {
            console.log(err)
            res.json({
                err,
                msg: "服务器异常",
                code: 503,
                type: 0
            })
        })
})

//只看已用 代理
router.get("/onlyshowuseddl", (req, res) => {
    var query = req.query;
    console.log(query)
    keyword.find({ isuse: 1, username: query.username })
        .then(result => {
            res.json({
                msg: "卡密查询成功",
                code: 200,
                result,
                type: 1,
            })
        }).catch(err => {
            console.log(err)
            res.json({
                err,
                msg: "服务器异常",
                code: 503,
                type: 0
            })
        })
})

//查询 单个卡密信息
router.get("/getonekeyword", (req, res) => {
    var query = req.query;
    console.log(query)
    keyword.findOne({ key: query.key })
        .then(result => {
            if (result) {
                res.json({
                    msg: "卡密查询成功",
                    code: 200,
                    result,
                    type: 1,
                })
            } else {
                res.json({
                    msg: "未查询到卡密",
                    code: 500,
                    type: 0
                })
            }
        }).catch(err => {
            console.log(err)
            res.json({
                err,
                msg: "服务器异常",
                code: 503,
                type: 0
            })
        })
})


//只看未用 管理
router.get("/onlyshownousedall", (req, res) => {
    var query = req.query;
    console.log(query)
    keyword.find({ isuse: 0 })
        .then(result => {
            res.json({
                msg: "卡密查询成功",
                code: 200,
                result,
                type: 1,
            })
        }).catch(err => {
            console.log(err)
            res.json({
                err,
                msg: "服务器异常",
                code: 503,
                type: 0
            })
        })
})

//只看未用 代理
router.get("/onlyshownouseddl", (req, res) => {
    var query = req.query;
    console.log(query)
    keyword.find({ isuse: 0, username: query.username })
        .then(result => {
            res.json({
                msg: "卡密查询成功",
                code: 200,
                result,
                type: 1,
            })
        }).catch(err => {
            console.log(err)
            res.json({
                err,
                msg: "服务器异常",
                code: 503,
                type: 0
            })
        })
})



//查询 用户交易信息
router.get("/getuserjiaoyi", (req, res) => {
    var query = req.query;
    console.log(query)
    fanli.find({ username: query.username })
        .then(result => {
            if (result) {
                res.json({
                    msg: "交易查询成功",
                    code: 200,
                    result,
                    type: 1,
                })
            } else {
                res.json({
                    msg: "未查询到交易",
                    code: 500,
                    type: 0
                })
            }
        }).catch(err => {
            console.log(err)
            res.json({
                err,
                msg: "服务器异常",
                code: 503,
                type: 0
            })
        })
})


//查询 用户交易信息
router.get("/getallbuy", (req, res) => {
    var query = req.query;
    console.log(query)
    fanli.find().sort({ time: -1 })
        .then(result => {
            if (result) {
                res.json({
                    msg: "交易查询成功",
                    code: 200,
                    result,
                    type: 1,
                })
            } else {
                res.json({
                    msg: "未查询到交易",
                    code: 500,
                    type: 0
                })
            }
        }).catch(err => {
            console.log(err)
            res.json({
                err,
                msg: "服务器异常",
                code: 503,
                type: 0
            })
        })
})


//查询 代理交易信息
router.get("/getalljiaoyi", (req, res) => {
    var query = req.query;
    console.log(query)
    fanli.find().sort({ time: -1 })
        .then(result => {
            if (result) {
                res.json({
                    msg: "交易查询成功",
                    code: 200,
                    result,
                    type: 1,
                })
            } else {
                res.json({
                    msg: "未查询到交易",
                    code: 500,
                    type: 0
                })
            }
        }).catch(err => {
            console.log(err)
            res.json({
                err,
                msg: "服务器异常",
                code: 503,
                type: 0
            })
        })
})


// 删除卡密
router.get("/delcarkey", (req, res) => {
    var query = req.query;
    console.log(query)
    keyword.deleteOne({
        key: query.key
    })
        .then(result => {
            res.json({
                msg: "删除用户成功",
                code: 200,
                result,
                type: 1,
            })
        }).catch(err => {
            console.log(err)
            res.json({
                err,
                msg: "服务器异常",
                code: 503,
                type: 0
            })
        })
})



// 删除
router.get("/deluserone", (req, res) => {
    var query = req.query;
    console.log(query)
    AdminUser.deleteOne({
        _id: query._id
    })
        .then(result => {
            res.json({
                msg: "删除用户成功",
                code: 200,
                result,
                type: 1,
            })
        }).catch(err => {
            console.log(err)
            res.json({
                err,
                msg: "服务器异常",
                code: 503,
                type: 0
            })
        })
})

// 添加
router.get("/adduser", (req, res) => {
    var query = req.query;
    console.log(query)
    var phone = query.phone
    var username = query.username
    AdminUser.findOne({ username }).then(result => {
        if (result) {
            res.json({
                msg: "用户名已存在",
                code: 500,
                type: 0,
            })
        } else {
            AdminUser.findOne({ phone }).then(resa => {
                if (resa) {
                    res.json({
                        msg: "手机号已存在",
                        code: 500,
                        type: 0,
                    })
                } else {
                    AdminUser.insertMany(query)
                        .then(data => {
                            res.json({
                                msg: "添加用户成功",
                                code: 200,
                                data,
                                type: 1,
                            })
                        }).catch(err => {
                            console.log(err)
                            res.json({
                                err,
                                msg: "服务器异常",
                                code: 503,
                                type: 0
                            })
                        })
                }
            })
        }
    })
})


// 用户返利
router.post("/userfanli", (req, res) => {
    var body = req.body;
    console.log(body)
    checkToken(req, res, ({ phone }) => {
        console.log(">>>")
        AdminUser.updateMany({
            username: body.username
        }, {
            $set: {
                money: body.money,
                usetime: new Date(),
            }
        }).then(result => {
            res.json({
                code: 200,   // 402  403 统一失败  
                msg: "返利添加成功",
                type: 1,
                result
            })

        }).catch(err => {
            res.json({
                code: 500,   // 402  403 统一失败  
                msg: "服务器异常- 未知错误",
                type: 0,
            })
        })
    })
})



// 卡密修改状态
router.post("/updatekeyword", (req, res) => {
    var body = req.body;
    console.log(body)
    checkToken(req, res, ({ phone }) => {
        keyword.updateMany({
            key: body.key
        }, {
            $set: {
                isuse: 1,
                usetime: new Date(),
            }
        }).then(result => {
            res.json({
                code: 200,   // 402  403 统一失败  
                msg: "卡密状态已修改",
                type: 1,
                result
            })
        }).catch(err => {
            res.json({
                code: 500,   // 402  403 统一失败  
                msg: "服务器异常- 未知错误",
                type: 0,
            })
        })
    })
})



// 用户修改  
router.post("/updateuserrole", (req, res) => {
    var body = req.body;
    console.log(body)
    checkToken(req, res, ({ phone }) => {
        AdminUser.updateMany({
            _id: body._id
        }, {
            $set: {
                password: body.password,
                phone: body.phone,
                money: body.money,
                username: body.username,
                role: body.role
            }
        }).then(result => {
            res.json({
                code: 200,   // 402  403 统一失败  
                msg: "权限修改成功",
                type: 1,
                result
            })
        }).catch(err => {
            res.json({
                code: 500,   // 402  403 统一失败  
                msg: "服务器异常- 未知错误",
                type: 0,
            })
        })
    })
})

// 修改用户金额
router.post("/updatemoney", (req, res) => {
    var body = req.body;
    console.log(body)
    checkToken(req, res, ({ phone }) => {
        AdminUser.updateMany({
            username: body.username
        }, {
            $set: {
                money: body.money,

            }
        }).then(result => {
            res.json({
                code: 200,   // 402  403 统一失败  
                msg: "成功",
                type: 1,
                result
            })
        }).catch(err => {
            res.json({
                code: 500,   // 402  403 统一失败  
                msg: "服务器异常- 未知错误",
                type: 0,
            })
        })
    })
})


// 插入返利公告
router.post("/addfanligongao", (req, res) => {
    var body = req.body;
    console.log(body)
    checkToken(req, res, ({ phone }) => {
        AdminUser.findOne({
            phone: phone
        }).then(data => {
            console.log(data)
            alone = body.alone,
                fanmoney = body.fanmoney,
                money = body.money,
                modl = body.modl
            body.username = data.username;
            body.time = new Date();
            fanli.insertMany(body)
                .then(result => {
                    res.json({
                        code: 200,   // 402  403 统一失败  
                        msg: "返利公告记录添加成功",
                        type: 1,
                        result
                    })
                })
        }).catch(err => {
            res.json({
                code: 500,   // 402  403 统一失败  
                msg: "服务器异常- 未知错误",
                type: 0,
            })
        })
    })
})



// 插入体温记录 
router.post("/addtiWen", (req, res) => {
    var body = req.body;
    console.log(body)
    checkToken(req, res, ({ phone }) => {
        AdminUser.findOne({
            phone: phone
        }).then(data => {
            console.log(data)
            body.phone = data.phone;
            body.username = data.username;
            body.time = new Date();
            tiWen.insertMany(body)
                .then(result => {
                    res.json({
                        code: 200,   // 402  403 统一失败  
                        msg: "体温记录添加成功",
                        type: 1,
                        result
                    })
                })
        }).catch(err => {
            res.json({
                code: 500,   // 402  403 统一失败  
                msg: "服务器异常- 未知错误",
                type: 0,
            })
        })
    })
})

//查询体温
router.post("/gettiwenlog", (req, res) => {
    var body = req.body;
    var obj = {}
    if (body.username) {
        obj.username = body.username
    }
    if (body.keyword) {
        obj = {
            $or: [
                { username: new RegExp(body.keyword) },  // 模糊搜索
                { phone: new RegExp(body.keyword) },
                // {tiwen:new RegExp(body.keyword)  } 
            ]
        }
    }
    checkToken(req, res, ({ phone }) => {
        tiWen.find(obj).sort({ _id: -1 }).then(result => {
            res.json({
                code: 200,   // 402  403 统一失败  
                msg: "体温记录查询成功",
                type: 1,
                result
            })
        }).catch(err => {
            console.log(err)
            res.json({
                code: 500,   // 402  403 统一失败  
                msg: "服务器异常- 未知错误",
                type: 0,
                err,
            })
        })
    })
})


//查询卡密  --管理员
router.post("/getwordreg", (req, res) => {
    var body = req.body

    keyword.find({ key: body.key }).then(result => {
        res.json({
            code: 200,   // 402  403 统一失败  
            msg: "卡密查询成功",
            type: 1,
            result
        })
    }).catch(err => {
        console.log(err)
        res.json({
            code: 500,   // 402  403 统一失败  
            msg: "服务器异常- 未知错误",
            type: 0,
            err,
        })
    })

})

//查询卡密  --代理
router.post("/getworddl", (req, res) => {
    var body = req.body

    keyword.find({ key: body.key, username: body.username }).then(result => {
        res.json({
            code: 200,   // 402  403 统一失败  
            msg: "卡密查询成功",
            type: 1,
            result
        })
    }).catch(err => {
        console.log(err)
        res.json({
            code: 500,   // 402  403 统一失败  
            msg: "服务器异常- 未知错误",
            type: 0,
            err,
        })
    })

})


//查询用户 --管理员
router.post("/getuseronegl", (req, res) => {
    var body = req.body

    AdminUser.find({ username: body.username }).then(result => {
        res.json({
            code: 200,   // 402  403 统一失败  
            msg: "用户查询成功",
            type: 1,
            result
        })
    }).catch(err => {
        console.log(err)
        res.json({
            code: 500,   // 402  403 统一失败  
            msg: "服务器异常- 未知错误",
            type: 0,
            err,
        })
    })

})

//查询用户  --代理
router.post("/getuseronedl", (req, res) => {
    var body = req.body
    console.log(body)
    AdminUser.find({ alone: body.alone, username: body.username }).then(result => {
        if (result) {
            res.json({
                code: 200,   // 402  403 统一失败  
                msg: "卡密查询成功",
                type: 1,
                result
            })
        } else {
            res.json({
                code: 300,   // 402  403 统一失败  
                msg: "未查询到",
                type: 0,

            })
        }

    }).catch(err => {
        console.log(err)
        res.json({
            code: 500,   // 402  403 统一失败  
            msg: "服务器异常- 未知错误",
            type: 0,
            err,
        })
    })

})

// 上传头像  
// multer 
var multer = require("multer");
var storage = multer.diskStorage({
    destination: function (req, file, cb) {   // 头像存放的目录 
        cb(null, './public/avatar')    // . 文件路径
    },
    filename: function (req, file, cb) {  // 新的文件名 
        cb(null, Date.now() + "WH2008" + file.originalname);  //  originalname 原文件名  1.jpg    145123213123WH20081.jpg
    }
})


var upload = multer({ storage: storage }).any();  // 可以上传任何格式的图片

router.post('/uploadavatar', upload, (req, res) => {
    checkToken(req, res, ({ phone }) => {
        if (req.files) {
            var path = req.files[0].path;
            AdminUser.updateMany({
                phone,
            }, {
                $set: {
                    avatar: path
                }
            }).then(result => {
                res.json({
                    code: 200,
                    msg: "头像上传成功",
                    avatar: path,
                    result
                })
            }).catch(err => {
                res.json({
                    code: 500,   // 500 统一失败  
                    msg: "服务器异常- 未知错误",
                    type: 0,
                })
            })
        }
    })
})


// 删除
router.get("/delgonggaoone", (req, res) => {
    var query = req.query;
    checkToken(req, res, ({ phone }) => {
        GongGao.remove({
            _id: query._id
        })
            .then(result => {
                res.json({
                    msg: "删除成功",
                    code: 200,
                    result,
                    type: 1,
                })
            }).catch(err => {
                console.log(err)
                res.json({
                    err,
                    msg: "服务器异常",
                    code: 503,
                    type: 0
                })
            })
    })
})


// 上传图片
router.post('/uploadanyimg', upload, (req, res) => {
    checkToken(req, res, ({ phone }) => {
        if (req.files) {
            var path = req.files[0].path;
            res.json({
                code: 200,
                msg: "上传成功",
                path,
                type: 1,
            })
        } else {
            res.json({
                code: 500,   // 500 统一失败  
                msg: "服务器异常- 未知错误",
                type: 0,
            })
        }
    })
})


// 插入 公告 
router.post("/addgonggaolog", (req, res) => {
    var body = req.body;
    // checkToken(req, res, ({ phone }) => {
    body.author = body.username,
        body.time = new Date();
    GongGao.insertMany(body)
        .then(result => {
            res.json({
                code: 200,   // 402  403 统一失败  
                msg: "添加成功",
                type: 1,
                result
            })
        }).catch(err => {
            res.json({
                code: 500,   // 402  403 统一失败  
                msg: "服务器异常- 未知错误",
                type: 0,
                err
            })
        })
    // })
})

// 查询
router.get("/getgonggaolist", (req, res) => {
    var query = req.query;
    checkToken(req, res, ({ phone }) => {
        GongGao.find()
            .sort({ _id: -1 })
            .then(result => {
                res.json({
                    msg: "查询成功",
                    code: 200,
                    result,
                    type: 1,
                })
            }).catch(err => {
                console.log(err)
                res.json({
                    err,
                    msg: "服务器异常",
                    code: 503,
                    type: 0
                })
            })
    })
})


// 添加投诉
router.post("/addtousu", (req, res) => {
    var body = req.body;
    body.time = new Date();
    tousu.insertMany(body)
        .then(result => {
            res.json({
                code: 200,   // 402  403 统一失败  
                msg: "投诉添加成功",
                type: 1,
                result
            })
        }).catch(err => {
            res.json({
                code: 500,   // 402  403 统一失败  
                msg: "服务器异常- 未知错误",
                type: 0,
                err
            })
        })

})

// 查询投诉  --管理
router.get("/gettousu", (req, res) => {
    var query = req.query;
    checkToken(req, res, ({ phone }) => {
        tousu.find()
            .sort({ _id: -1 })
            .then(result => {
                res.json({
                    msg: "查询成功",
                    code: 200,
                    result,
                    type: 1,
                })
            }).catch(err => {
                console.log(err)
                res.json({
                    err,
                    msg: "服务器异常",
                    code: 503,
                    type: 0
                })
            })
    })
})


// 查询投诉  --代理
router.get("/gettousudl", (req, res) => {
    var query = req.query;
    console.log(query)
    checkToken(req, res, ({ phone }) => {
        tousu.find({ username: query.username })
            .sort({ _id: -1 })
            .then(result => {
                res.json({
                    msg: "查询成功",
                    code: 200,
                    result,
                    type: 1,
                })
            }).catch(err => {
                console.log(err)
                res.json({
                    err,
                    msg: "服务器异常",
                    code: 503,
                    type: 0
                })
            })
    })
})

// 新增回复
router.post("/addhuifu", (req, res) => {
    var body = req.body;
    body.time = new Date();
    huifu.insertMany(body)
        .then(result => {
            res.json({
                code: 200,   // 402  403 统一失败  
                msg: "回复成功",
                type: 1,
                result
            })
        }).catch(err => {
            res.json({
                code: 500,   // 402  403 统一失败  
                msg: "服务器异常- 未知错误",
                type: 0,
                err
            })
        })
})



// 修改投诉状态
router.post("/changeanswer", (req, res) => {
    var body = req.body;
    console.log(body)
    checkToken(req, res, ({ phone }) => {
        tousu.updateMany({
            _id: body._id
        }, {
            $set: {
                answer: 1,
            }
        }).then(result => {
            res.json({
                code: 200,   // 402  403 统一失败  
                msg: "卡密状态已修改",
                type: 1,
                result
            })
        }).catch(err => {
            res.json({
                code: 500,   // 402  403 统一失败  
                msg: "服务器异常- 未知错误",
                type: 0,
            })
        })
    })
})


//查询回复
router.get("/findhuifu", (req, res) => {
    var query = req.query;
    console.log(query)
    checkToken(req, res, ({ phone }) => {
        huifu.find({ answerid: query.answerid })
            .sort({ _id: -1 })
            .then(result => {
                res.json({
                    msg: "查询成功",
                    code: 200,
                    result,
                    type: 1,
                })
            }).catch(err => {
                console.log(err)
                res.json({
                    err,
                    msg: "服务器异常",
                    code: 503,
                    type: 0
                })
            })
    })
})

//查询排行
router.get("/findmoney", (req, res) => {
    var query = req.query;
    console.log(query)
    checkToken(req, res, ({ phone }) => {
        AdminUser.find()
            .sort({ money: -1 })
            .then(result => {
                res.json({
                    msg: "查询成功",
                    code: 200,
                    result,
                    type: 1,
                })
            }).catch(err => {
                console.log(err)
                res.json({
                    err,
                    msg: "服务器异常",
                    code: 503,
                    type: 0
                })
            })
    })
})

module.exports = router